package com.juyu.miliao.api.service;

import com.juyu.miliao.api.constant.GameDataRedisKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;

import static com.juyu.miliao.api.constant.GameDataRedisKey.STAT_GAME_EXPIRE_NUM;

/**
 * @ClassName: GameDataService
 * @Description: TODO
 * @Author zhangcheng
 * @Date 19-9-19上午9:40
 */
@Service
@Slf4j
public class GameDataService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 访问人数 日期
     */
    public void increaseGameVisitNum(LocalDate date, Long userId) {
        String key = GameDataRedisKey.getStatGameVisitNumDate(date);
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 所有访问人数
     */
    public void increaseGameVisitNumAll(Long userId) {
        String key = GameDataRedisKey.getStatGameVisitNumAll();
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 增加签到人数
     */
    public void increaseSignInUserCount(LocalDate date, Long userId) {
        String key = GameDataRedisKey.getStatSignUpNumDate(date);
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 增加签到领取豆子
     */
    public void increaseSignInAwardBeans(LocalDate date, Long beans) {
        String key = GameDataRedisKey.getStatSignUpExpendDate(date);
        stringRedisTemplate.opsForValue().increment(key, beans);
    }

    /**
     * 增加vip累签领奖人数
     */
    public void increaseVipSignInUserCount(LocalDate date, Long userId) {
        String key = GameDataRedisKey.getStatSignUpVipNumDate(date);
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 增加vip累签领取豆子
     */
    public void increaseVipSignInAwardBeans(LocalDate date, Long beans) {
        String key = GameDataRedisKey.getStatSignUpVipExpendDate(date);
        stringRedisTemplate.opsForValue().increment(key, beans);
    }

    /**
     * 救济豆人数
     */
    public void increaseAgateNum(LocalDate date, Long userId) {
        String key = GameDataRedisKey.getStatAgateNumDate(date);
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 救济豆支出
     */
    public void increaseAgateExpend(LocalDate date, Long beans) {
        String key = GameDataRedisKey.getStatAgateExpendDate(date);
        stringRedisTemplate.opsForValue().increment(key, beans);
    }

    /**
     * 聚宝盆人数
     */
    public void increaseReliefNum(LocalDate date, Long userId) {
        String key = GameDataRedisKey.getStatReliefNumDate(date);
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 聚宝盆支出
     */
    public void increaseReliefExpend(LocalDate date, Long beans) {
        String key = GameDataRedisKey.getStatReliefExpendDate(date);
        stringRedisTemplate.opsForValue().increment(key, beans);
    }

    /**
     * 任务领奖人数
     */
    public void increaseNoobMissionNum(LocalDate date, Long userId) {
        String key = GameDataRedisKey.getStatNoobMissionNumDate(date);
        stringRedisTemplate.opsForHyperLogLog().add(key, String.valueOf(userId));
    }

    /**
     * 任务支出
     */
    public void increaseNoobMissionExpend(LocalDate date, Long beans) {
        String key = GameDataRedisKey.getStatNoobMissionExpendDate(date);
        stringRedisTemplate.opsForValue().increment(key, beans);
    }

    /**
     * 清除统计缓存
     */
    @Scheduled(cron = "0 0 3 1/1 * ?")
    public void cleanKey() {
        LocalDate date = LocalDate.now().minusDays(STAT_GAME_EXPIRE_NUM);
        log.info("stat:game:清除统计缓存:start:date={}", date);
        stringRedisTemplate.delete(GameDataRedisKey.getStatGameVisitNumDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatSignUpNumDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatSignUpExpendDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatSignUpVipNumDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatSignUpVipExpendDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatAgateNumDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatAgateExpendDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatReliefNumDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatReliefExpendDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatNoobMissionNumDate(date));
        stringRedisTemplate.delete(GameDataRedisKey.getStatNoobMissionExpendDate(date));
        log.info("stat:game:清除统计缓存:end");
    }
}
